package com.antu.first.service.impl;

import com.antu.first.bean.DiscountCoupon;
import com.antu.first.bean.Hotel;
import com.antu.first.bean.User;
import com.antu.first.dao.HotelDao;
import com.antu.first.dao.UserDao;
import com.antu.first.dto.UserOperationExecution;
import com.antu.first.enums.UserStateEnum;
import com.antu.first.exceptions.UserOperationException;
import com.antu.first.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Autowired
    private HotelDao hotelDao;
    Map<String, Object> map=new HashMap<String, Object>();

    @Override
    public Map<String, Object> selectAllDiscountCouponByUid(DiscountCoupon dis) throws Exception {
        int a=-1;
        int uId=dis.getUser().getuId();
        int n=0;
        List<DiscountCoupon> list1=null;
        List<DiscountCoupon> list=userDao.selectAllDiscountCouponByUid(dis);
        for(int i=0;i<list.size();i++){
            Date dStartTime=list.get(i).getdStart();
            Date dStopTime=list.get(i).getdStop();
            if(dStartTime.getTime()>dis.getdStart().getTime()){
                a=0;
            } else if (dStartTime.getTime()<dis.getdStart().getTime()&&dis.getdStart().getTime()<dStopTime.getTime()){
                a=1;
            } else if (dStopTime.getTime()<dis.getdStart().getTime()){
                a=2;
            }
            n=userDao.updateUserDiscountCouponStateByDate(a,uId,dis.getdStart());
            n++;
        }
        if (n>0){
            list1=userDao.selectAllDiscountCouponByUid(dis);
            if(list1!=null){
                map.put("list",list1);
            }else {
                map.put("errMsg","暂无此类优惠券");
            }
        }else {
            map.put("errMsg","优惠券状态码修改错误");
        }
        return map;
    }

    @Override
    public User selectUserByOpenId(User user) throws Exception{
        return userDao.selectUserByOpenId(user);
    }

    @Override
    public UserOperationExecution addUser(User user) {
        //新增用户，在该方法中，将设置用户账号的基础信息
        if(user != null){
            user.setuAggregate(0);//设置当前消费总额
            user.setuCreateTime(new Date());//设置账号创建时间
            user.setuLastEditTime(new Date());//设置账号最后修改时间
            user.setuTotalIntegral(0);//设置总积分
            user.setuPresentIntegral(0);//设置当前积分
            user.setGrade(0);//设置用户等级
            try{
                int effectNum = userDao.addUser(user);
                if(effectNum != 1){
                    return new UserOperationExecution(UserStateEnum.REGISTER_FAILED);
                }
            }catch(Exception e){
                throw new UserOperationException("UserServiceImpl-addUser,发生异常："+e.getMessage());
            }
        }else{
            return new UserOperationExecution(UserStateEnum.EMPTY_INFO);
        }
        return new UserOperationExecution(UserStateEnum.REGISTER_SUCCESS, user);
    }
}
